package com.ocom.webapi.controller;


import com.ocom.common.constant.WebApiServiceUrl;
import com.ocom.common.request.payconfig.AddTollPointInfoRequest;
import com.ocom.common.request.payconfig.GetTollPointInfoRequest;
import com.ocom.common.request.payconfig.UpdateTollPointInfoRequest;
import com.ocom.common.request.web.DelTollPointRequest;
import com.ocom.common.request.web.TollPointPageAddRequest;
import com.ocom.common.request.web.TollPointPageModifyRequest;
import com.ocom.common.request.web.TollPointPageOnShowRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.security.authentication.YoCiUser;
import com.ocom.security.utils.SecurityUtils;
import com.ocom.webapi.feign.PayConfigClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
@PreAuthorize("@pms.hasRole('ROLE_WEB')" )
public class TollPointPageController {

	@Autowired
	PayConfigClientService payConfigClientService;
	/**
	 * 2.3.1收费点列表（TollPointPage/onShow）
	 */
	@PostMapping(WebApiServiceUrl.URL_TollPointPageOnShow)
	public Result TollPointPageOnShow(@Valid @RequestBody TollPointPageOnShowRequest request){
		GetTollPointInfoRequest request1=new GetTollPointInfoRequest();
		Integer merchId = (request.getMerchId()==null || request.getMerchId()==0)?-1:request.getMerchId();
		request1.setMerch_id(merchId);
		YoCiUser yoCiUser = SecurityUtils.getYoCiUser();
		request1.setCom_id(yoCiUser.getComId());
		request1.setPoint_name(request.getPointName());
		request1.setPage(request.getPage());
		request1.setSize(request.getSize());
	//	System.out.println(request1);
		return payConfigClientService.getTollPointInfo(request1);
	}

	//2.3.2收费点新增（TollPointPage/add）
	@PostMapping(WebApiServiceUrl.URL_TollPointPageAdd)
	public Result TollPointPageAdd(@Valid @RequestBody TollPointPageAddRequest request){
		YoCiUser yoCiUser = SecurityUtils.getYoCiUser();
		AddTollPointInfoRequest addTollPointInfoRequest=new AddTollPointInfoRequest();
		addTollPointInfoRequest.setComId(yoCiUser.getComId());
		addTollPointInfoRequest.setMerchId(request.getMerchId());
		addTollPointInfoRequest.setPointName(request.getPointName());
		addTollPointInfoRequest.setRemark(request.getRemark());
		addTollPointInfoRequest.setParentPointId(request.getParentPointId());
		return payConfigClientService.addTollPointInfo(addTollPointInfoRequest);
	}

	//2.3.2收费点新增（TollPointPage/add）
	@PostMapping(WebApiServiceUrl.URL_TollPointPageModify)
	public Result TollPointPageModify(@Valid @RequestBody TollPointPageModifyRequest request) {
		YoCiUser yoCiUser = SecurityUtils.getYoCiUser();
		UpdateTollPointInfoRequest updateTollPointInfoRequest=new UpdateTollPointInfoRequest();
		updateTollPointInfoRequest.setCom_id(yoCiUser.getComId());
		updateTollPointInfoRequest.setPoint_id(request.getPointId());
		updateTollPointInfoRequest.setMerch_id(request.getMerchId());
		updateTollPointInfoRequest.setPoint_name(request.getPointName());
		updateTollPointInfoRequest.setParent_point_id(request.getParentPointId());
		updateTollPointInfoRequest.setPoint_orgin(request.getPointOrgin());
		updateTollPointInfoRequest.setRemark(request.getRemark());
		return payConfigClientService.updateTollPointInfo(updateTollPointInfoRequest);
	}

	@PostMapping(WebApiServiceUrl.URL_TollPointPageDelete)
	public Result TollPointPageModify(@Valid @RequestBody DelTollPointRequest request) {
		YoCiUser yoCiUser = SecurityUtils.getYoCiUser();
		request.setComId(yoCiUser.getComId());
		payConfigClientService.deleteTollPointInfo(request);
		return ResultUtil.success();
	}

}
